package edu.hm.devcamp.communication.xmlparser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;
import edu.hm.devcamp.presentation.activities.PoiMap;
import edu.hm.devcamp.providers.Poi;

/**
 * @author johannes
 */
public class ModgyHandler extends DefaultHandler {
    
	 // names of the XML tags
    static final String DATA = "data";
    static final String POIS = "pois";
    static final String POI = "poi";
    static final String LANG = "lang";
    static final String RESULT = "result";
    static final String CATEGORY = "category";
    static final String NAME = "name";
    static final String VALUE = "value";
    static final String DESCRIPTION = "description";
    static final String STREETNAME = "streetname";
    static final String STREETNUMBER = "streetnumber";
    static final String POSTCODE = "postcode";
    static final String CITY = "city";
    static final String COORDINATES = "coordinates";
    static final String LONGITUDE = "longitude";
    static final String LATIDUDE = "latidude";
    static final String URL = "url";
	
	private List<Poi> mPois;
    private Poi mCurrentPoi;
    private StringBuilder builder;
    private int value = 0;
    
    private String parentElement = "";
    private String langAttribute = "";
    
    public List<Poi> getPOIs(){
        return mPois;
    }
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);
        builder.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        super.endElement(uri, localName, name);
        if (mCurrentPoi != null){
            if (localName.equalsIgnoreCase("streetname")){
                mCurrentPoi.street = builder.toString().trim();
            } else if (localName.equalsIgnoreCase("streetnumber")){
                mCurrentPoi.streetnumber = builder.toString().trim();
            } else if (localName.equalsIgnoreCase("postcode")){
                mCurrentPoi.zipcode = builder.toString().trim();
            } else if (localName.equalsIgnoreCase(CATEGORY)){
                mCurrentPoi.category = builder.toString().trim();
            } else if (localName.equalsIgnoreCase("longitude")){
                mCurrentPoi.longitude = Float.parseFloat(builder.toString().trim().replace(",", "."));
            } else if (localName.equalsIgnoreCase("latitude")){
                mCurrentPoi.latitude = Float.parseFloat(builder.toString().trim().replace(",", "."));
            } else if (localName.equalsIgnoreCase("id")){
                mCurrentPoi.externalId = builder.toString().trim();
            } else if (localName.equalsIgnoreCase(URL)){
                mCurrentPoi.url = builder.toString().trim();
            } else if (localName.equalsIgnoreCase("value")){
            	if (parentElement.equalsIgnoreCase(NAME)) {
            	    if(PoiMap.LOG_D){
            	        Log.d("MogdyHandler", "----> name: " + builder.toString().trim());
            	    }
            		mCurrentPoi.name = builder.toString().trim();
            	} else if (parentElement.equalsIgnoreCase(DESCRIPTION)){
            	    if(PoiMap.LOG_D){
                        Log.d("MogdyHandler", "----> desc: " + builder.toString().trim());
            	    }
            	    mCurrentPoi.description = builder.toString().trim();
            	} else if (parentElement.equalsIgnoreCase(CITY)){
            	    if(PoiMap.LOG_D){
                        Log.d("MogdyHandler", "----> city: " + builder.toString().trim());
            	    }
            	    mCurrentPoi.city = builder.toString().trim();
            	}

            } else if (localName.equalsIgnoreCase("poi")){
                mPois.add(mCurrentPoi);
            }
            builder.setLength(0);    
        }
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        mPois = new ArrayList<Poi>();
        builder = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        super.startElement(uri, localName, name, attributes);
        if (localName.equalsIgnoreCase("poi")){
            mCurrentPoi = new Poi();
        } else if (localName.equalsIgnoreCase(VALUE)){
        	langAttribute = attributes.getValue("lang");
        } else if (localName.equalsIgnoreCase(NAME)) {
        	parentElement = NAME;
        } else if (localName.equalsIgnoreCase(DESCRIPTION)) {
        	parentElement = DESCRIPTION;
        } else if (localName.equalsIgnoreCase(CITY)) {
        	parentElement = CITY;
        }
        
    }
}